﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using System.Xml.Serialization;

namespace TodoLister.Models
{
    [Serializable]
    public class TodoItem : INotifyPropertyChanged
    {
        private const int CURRENT_VERSION = 1;
        private string _title;
        private string _content;
        private DateTime _createdDate;
        private bool _isCompleted;
        private TodoPriority _priority;

        /// <summary>
        /// 数据版本
        /// </summary>
        [XmlAttribute]
        public int Version { get; set; } = CURRENT_VERSION;

        /// <summary>
        /// 唯一标识符
        /// </summary>
        [XmlAttribute]
        public Guid Id { get; set; } = Guid.NewGuid();

        /// <summary>
        /// 标题
        /// </summary>
        [XmlElement]
        public string Title
        {
            get => _title;
            set
            {
                if (_title != value)
                {
                    _title = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 内容
        /// </summary>
        [XmlElement]
        public string Content
        {
            get => _content;
            set
            {
                if (_content != value)
                {
                    _content = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 创建时间
        /// </summary>
        [XmlElement]
        public DateTime CreatedDate
        {
            get => _createdDate;
            set
            {
                if (_createdDate != value)
                {
                    _createdDate = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 是否完成
        /// </summary>
        [XmlElement]
        public bool IsCompleted
        {
            get => _isCompleted;
            set
            {
                if (_isCompleted != value)
                {
                    _isCompleted = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 优先级
        /// </summary>
        [XmlElement]
        public TodoPriority Priority
        {
            get => _priority;
            set
            {
                if (_priority != value)
                {
                    _priority = value;
                    OnPropertyChanged();
                }
            }
        }

        /// <summary>
        /// 属性更改事件
        /// </summary>
        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// 触发属性更改通知
        /// </summary>
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public TodoItem()
        {
            CreatedDate = DateTime.Now;
            Priority = TodoPriority.None;
            Version = CURRENT_VERSION;
        }

        /// <summary>
        /// 带参数的构造函数
        /// </summary>
        public TodoItem(string title, string content)
        {
            Title = title;
            Content = content;
            CreatedDate = DateTime.Now;
            Priority = TodoPriority.None;
            Version = CURRENT_VERSION;
        }

        [XmlIgnore]
        public ICommand ShowMenuCommand { get; set; }
    }
}
